<HTML><HEAD>
<!--
    -----------------
    Strings as Arrays
    -----------------
-->

<SCRIPT LANGUAGE="JavaScript"><!-- hide from old browsers

/*
    THE JAVASCRIPT COOKBOOK by Erica Sadun, webrx@mindspring.com
    Copyright (c)2000 by Charles River Media.  All Rights Reserved.
    
    This applet can only be re-used or modifed by license holders of the
    JavaScript Cookbook CD-ROM.  Credit must be given in the source
    code and this copyright notice must be maintained. If you do
    not hold a license to the JavaScript Cookbook, you may NOT
    duplicate or modify this code for your own use.

    Use at your own risk. No warranty is given or implied of the suitability 
    of this applet for any specific application. Neither Erica Sadun nor 
    Charles River Media will be held responsible for any unwanted effects 
    due to the use of this applet or any derivative. 
*/


//------------------STRING-ARRAY UTILITIES-------------------

// Find the substring at index n, counting 0 to n
function doIndex(aString, n)
{
    
    var str=""+aString
    
    // Count until the correct index
    for(var i = 0; i < n; i++)
    {
        var where = str.indexOf(':', 1)
        str = str.substring(where+1, str.length)
    }

    // Lop off the end of the string
    return str.substring(0, str.indexOf(':'))
}

// Month string
var months = "January:February:March:April:May:June:July:"+
        "August:September:October:November:December:"

function choose()
{
    document.forms[0].month.value=
        doIndex(months, document.forms[0].input.options.selectedIndex)
}

<!-- done hiding --></SCRIPT></HEAD>

<BODY bgcolor="ffffff" link="0000ff" vlink="770077"
    onLoad="document.forms[0].input.focus();document.forms[0].input.select()">

<FONT COLOR="007777"><H1><IMG SRC="../GRAFX/UTENS.JPG" WIDTH=80 HEIGHT=50
    ALIGN = CENTER>Treating a String as an Array</H1></FONT>

<FONT COLOR="770000">
    Sometimes it is easier to store information in strings 
    rather than arrays. This saves lines of code and increases
    maintainability. This example
    shows one way to store month names in a string and index
    them.
</FONT></BLOCKQUOTE>

<BR><BR>

</SCRIPT><CENTER><FORM><TABLE BORDER=1>

<TR>
<TD align=center  colspan=3><input type="text" name="month" value="" 
    size=12">Month</TD>
</TR>

<TR>
<TD align=center  colspan=3>Number:
<SELECT    NAME="input" SIZE="1">
    <OPTION>1
    <OPTION>2
    <OPTION>3
    <OPTION>4
    <OPTION>5
    <OPTION>6
    <OPTION>7
    <OPTION>8
    <OPTION>9
    <OPTION>10
    <OPTION>11
    <OPTION>12
</SELECT><p>
</TD>
</TR>

<TR>
<TD align=center colspan=3>
<input type="button" value="Lookup Indexed Month" onClick="choose()"></TD>
</TR> 

</TABLE></FORM></CENTER>

<br><br>
<FONT COLOR="007777"><H2>Discussion</H2></FONT>
<FONT SIZE=4>
    Reuse this routine for any data-collection strings. This routine
    may be somewhat awkward, but it collapses data information into
    a compact, convenient representation and uses a minimum of extra
    memory.
</FONT>

<FONT COLOR="770000"><PRE>
// Month string
var months = "January:February:March:April:May:June:July:"+
        "August:September:October:November:December:"

// Find the substring at index n, counting 0 to n
function doIndex(aString, n)
{
    
    var str=""+aString
    
    // Count until the correct index
    for(var i = 0; i < n; i++)
    {
        var where = str.indexOf(':', 1)
        str = str.substring(where+1, str.length)
    }

    // Lop off the end of the string
    return str.substring(0, str.indexOf(':'))
}

function choose()
{
    document.forms[0].month.value=
        doIndex(months, document.forms[0].input.options.selectedIndex)
}
</PRE></FONT>
<h5>Copyright &copy;1996 by Charles River Media, All Rights Reserved</h5>
</BODY>
</HTML>